﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DQYT.HTDZ.BLL;
using System.Data;

namespace DQYT.HTDZ.Web.Views.SystemManage
{
    public partial class RoleManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                try
                {

                    BDMenuBLL menuBLL = new BDMenuBLL();
                    lblPath.Text = menuBLL.GetTitlePath(Request["mnu_id"]);

                    QueryData("");
                }
                catch (Exception ex)
                {
                    
                }
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="strName">角色名</param>
        private void QueryData(string strName)
        {
            DataTable dt = new DataTable();
            BDRoleBLL dJiaoSe = new BDRoleBLL();
            dt = dJiaoSe.GetData(" j.jse_name like'%" + strName + "%'");

            if (dt == null || dt.Rows.Count < 1)
            {
                dt.Rows.Add(dt.NewRow());
            }

            gvData.DataSource = dt;
            gvData.DataKeyNames = new string[] { "JSE_ID" };
            gvData.Columns[0].Visible = false;
            gvData.DataBind();   
        }

        protected void lnkbtnQXSZ_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                string id = (sender as LinkButton).CommandArgument;
                Response.Redirect("RoleSet.aspx?id=" + id);
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }
            #endregion
        }

        protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果没有数据则添加一行“没有数据”行
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string sRow = e.Row.Cells[2].Text.Trim().ToString().Replace("&nbsp;", "");

                    if (string.IsNullOrEmpty(sRow))
                    {
                        return;
                    }
                    e.Row.Cells[1].Text = (e.Row.RowIndex + 1).ToString();

                    string sRS = e.Row.Cells[3].Text.Trim().ToString().Replace("&nbsp;", "");
                    if (sRS != "" && sRS != "0")
                    {
                        CheckBox ck = e.Row.Cells[6].FindControl("ckFlag") as CheckBox;
                        ck.Enabled = false;
                    }

                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message.ToString());
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            Response.Redirect("Role.aspx?action=add");

        }
        protected void btnDel_Click(object sender, EventArgs e)
        {
            #region

            try
            {
                int n = 0;
                if (this.gvData.Rows.Count > 0)
                {
                    BDRoleBLL dJiaoSe = new BDRoleBLL();
                    for (int i = 0; i < gvData.Rows.Count; i++)
                    {
                        CheckBox ck = gvData.Rows[i].Cells[6].FindControl("ckFlag") as CheckBox;
                        if (ck.Checked == true && ck.Enabled == true)
                        {
                            ++n;
                            string strID = gvData.DataKeys[i]["JSE_ID"].ToString();
                            int iJSE_ID = Convert.ToInt32(strID);
                            dJiaoSe.Delete(iJSE_ID);
                        }
                    }
                    if (n == 0)
                    {
                        ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                    }
                    else
                    {
                        QueryData(txtName.Text.Trim());
                        lblMsg.Text = "成功删除 " + n.ToString() + " 个用户！";
                    }

                }
            }
            catch (Exception ex)
            {
                
            }

            #endregion
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            #region

            try
            {
                if (!string.IsNullOrEmpty(txtName.Text))
                {
                    QueryData(txtName.Text.Trim());
                }
            }
            catch (Exception ex)
            {
                
            }

            #endregion
        }
        protected void lnkbtnEdit_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                string id = (sender as LinkButton).CommandArgument;
                Response.Redirect("Role.aspx?id=" + id + "&action=update");
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }
            #endregion
        }

        protected void gvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvData.PageIndex = e.NewPageIndex;
            QueryData("");
        }
    }
}